package com.ruoyi.cmdb.controller;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.ruoyi.cmdb.domain.vo.CmdbModelsVo;
import com.ruoyi.cmdb.domain.vo.CmdbModelsImportVo;
import com.ruoyi.cmdb.domain.bo.CmdbModelsBo;
import com.ruoyi.cmdb.listener.CmdbModelsImportListener;
import com.ruoyi.cmdb.service.ICmdbModelsService;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.orm.core.page.TableDataInfo;

/**
 * 模型管理Controller
 *
 * @author mfq
 * 2024-11-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cmdb/models")
public class CmdbModelsController extends BaseController
{
    @Resource
    private ICmdbModelsService cmdbModelsService;

    /**
     * 查询模型管理列表
     */
    @SaCheckPermission("cmdb:models:list")
    @GetMapping("/list")
    public TableDataInfo<CmdbModelsVo> list(CmdbModelsBo cmdbModelsBo)
    {
        return cmdbModelsService.selectPage(cmdbModelsBo);
    }

    /**
     * 导出模型管理列表
     */
    @SaCheckPermission("cmdb:models:export")
    @Log(title = "模型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmdbModelsBo cmdbModelsBo)
    {
        List<CmdbModelsVo> list = cmdbModelsService.selectList(cmdbModelsBo);
        ExcelUtil.exportExcel(list, "模型管理", CmdbModelsVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "模型管理", businessType = BusinessType.IMPORT)
    @SaCheckPermission("cmdb:models:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<CmdbModelsImportVo> result = ExcelUtil.importExcel(file.getInputStream(), CmdbModelsImportVo.class, new CmdbModelsImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("cmdb:models:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "模型管理", CmdbModelsImportVo.class, response);
    }

    /**
     * 获取模型管理详细信息
     */
    @SaCheckPermission("cmdb:models:query")
    @GetMapping(value = "/{id}")
    public R<CmdbModelsVo> getInfo(@PathVariable String id)
    {
        return R.ok(cmdbModelsService.selectById(id));
    }

    /**
     * 新增模型管理
     */
    @SaCheckPermission("cmdb:models:add")
    @Log(title = "模型管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody CmdbModelsBo cmdbModelsBo)
    {
        boolean inserted = cmdbModelsService.insert(cmdbModelsBo);
        if (!inserted) {
            return R.fail("新增模型管理记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改模型管理
     */
    @SaCheckPermission("cmdb:models:edit")
    @Log(title = "模型管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody CmdbModelsBo cmdbModelsBo)
    {
        Boolean updated = cmdbModelsService.update(cmdbModelsBo);
        if (!updated) {
            return R.fail("修改模型管理记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除模型管理
     */
    @SaCheckPermission("cmdb:models:remove")
    @Log(title = "模型管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable String[] ids)
    {
        boolean deleted = cmdbModelsService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除模型管理记录失败!");
        }
        return R.ok();
    }
}
